package com.lgy.web.interceptor;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.lgy.model.User;
import com.lgy.util.CommonConstant;
import com.lgy.util.StringUtils;

/**
 * 
 * @author fengchao
 * SpringMVC拦截器类定义方式一：     实现HandlerInterceptor接口
 */
public class SpringMVCInterceptor implements HandlerInterceptor {
	private static Logger logger = LoggerFactory.getLogger(SpringMVCInterceptor.class);
	/**
	* 免登入 免检查地址
	*/
	private List<String> uncheckUrls; 
	public void setUncheckUrls(List<String> uncheckUrls) {
		this.uncheckUrls = uncheckUrls;
	}

	/**
	 * <p>Title: preHandle</p>  
	 * <p>Description: 该方法将在Controller处理之前进行调用</p>  
	 * @param request
	 * @param response
	 * @param handler
	 * @return   返回值true表示请求链链继续执行，false请求结束
	 * @throws Exception  
	 * @see org.springframework.web.servlet.HandlerInterceptor#preHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)
	 */
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		logger.info("preHandle >>>>>>>>>>>>>>> :" + "处理之前");
		String requestUrl = request.getRequestURI(); 
		String contextPath = request.getContextPath();
		
		requestUrl = StringUtils.removeStart(requestUrl, contextPath);
		if(!uncheckUrls.contains(requestUrl)) {
			HttpSession session = request.getSession();
			User user = (User) session.getAttribute(CommonConstant.USER_CONTEXT);
			if(user == null) {
				response.sendRedirect(contextPath  + "/" + "login.jsp");
				return false;
			}
		}
		return true;
	}
	
	/**
	 * <p>Title: postHandle</p>  
	 * <p>Description: Controller处理结束后执行,只有当preHandle返回为true才可以 </p>  
	 * @param request
	 * @param response
	 * @param handler
	 * @param modelAndView
	 * @throws Exception  
	 * @see org.springframework.web.servlet.HandlerInterceptor#postHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.web.servlet.ModelAndView)
	 */
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		logger.info("postHandle >>>>>>>>>>>>>>> :" + "Controller处理后");
	}
	
	/**
	 * <p>Title: afterCompletion</p>  
	 * <p>Description: springMVC视图渲染之后开始执行,只有当preHandle返回为true才可以,主要作用是用于清理资源的</p>  
	 * @param request
	 * @param response
	 * @param handler
	 * @param ex
	 * @throws Exception  
	 * @see org.springframework.web.servlet.HandlerInterceptor#afterCompletion(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, java.lang.Exception)
	 */
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		logger.info("afterCompletion >>>>>>>>>>>>>>> :" + "返回视图后");
	}

}
